package com.backedserver.controller.system;

import com.common.requestvo.system.RoleMenuReqVo;
import com.common.requestvo.system.RoleReqVo;
import com.common.service.system.RoleMenuService;
import com.common.utils.PageUtil;
import com.common.utils.ResultVoUtil;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.system.RoleResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色Controller
 *
 * @author johnny
 * @create 2020-07-13 下午12:51
 **/
@Api(tags = "角色菜单模块")
@RestController
@RequestMapping("/roleMenu")
@Slf4j
public class RoleController {


    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 查询 根据 角色Id 查询 该角色关联的menuIds
     *
     * @param roleId
     * @return
     */
    @GetMapping("/findRoleMenuIds")
    @ApiOperation(value = "根据角色Id 查询该角色关联的 menuIds")
    public ResultVo findRoleMenuIds(@RequestParam("roleId") Long roleId) {
        List<Long> menuIds = roleMenuService.findMenuIdsByRoleId(roleId);
        return ResultVoUtil.success(menuIds);
    }

    @PostMapping("/roleMenuSubmit")
    @ApiOperation(value = "提交角色Id 和 菜单的关系")
    public ResultVo roleMenuSubmit(@RequestBody RoleMenuReqVo roleMenuReqVo) {
        log.info("【roleMenuSubmit : {}】", roleMenuReqVo);

        roleMenuService.roleMenuSubmit(roleMenuReqVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/findRole")
    @ApiOperation(value = "角色分页接口")
    public ResultVo<Page<RoleResultVo>> findRolePage(@RequestParam(value = "roleName", required = false) String roleName, PageVo pageVo) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        Page<RoleResultVo> page = roleMenuService.findRolePage(PageUtil.initPage(pageVo), roleName);
        return ResultVoUtil.success(page);
    }

    @PostMapping("/addRole")
    @ApiOperation(value = "新增角色")
    public ResultVo addRole(@RequestBody RoleReqVo roleReqVo) {
        log.info("【add roleReqVo : {}】", roleReqVo);
        roleMenuService.addRole(roleReqVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/editRole")
    @ApiOperation(value = "编辑角色")
    public ResultVo editRole(@RequestBody RoleReqVo roleReqVo) {
        log.info("【edit roleReqVo : {}】", roleReqVo);
        if (roleReqVo.getId() == null) {
            return ResultVoUtil.error("can not id null");
        }
        roleMenuService.editRole(roleReqVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/deleteRole/{id}")
    @ApiOperation(value = "删除角色")
    public ResultVo deleteRole(@PathVariable("id") Long id) {
        log.info("【delete roleId : {}】", id);
        if (id == null) {
            return ResultVoUtil.error("can not id null");
        }
        roleMenuService.deleteRole(id);
        return ResultVoUtil.success();
    }
}